package com.example.permissionservice.controller;

import com.example.common.dto.ApiResponse;
import com.example.common.feign.PermissionFeignClient;
import com.example.permissionservice.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/permission")
public class PermissionController implements PermissionFeignClient {
    
    private final PermissionService permissionService;
    
    @Autowired
    public PermissionController(PermissionService permissionService) {
        this.permissionService = permissionService;
    }

    @Override
    @PostMapping("/bind-default-role")
    public ApiResponse<Void> bindDefaultRole(@RequestParam("userId") Long userId) {
        permissionService.bindDefaultRole(userId);
        return ApiResponse.success(null);
    }

    @Override
    @GetMapping("/user-role")
    public ApiResponse<String> getUserRoleCode(@RequestParam("userId") Long userId) {
        String roleCode = permissionService.getUserRoleCode(userId);
        return ApiResponse.success(roleCode);
    }

    @Override
    @PostMapping("/upgrade-to-admin")
    public ApiResponse<Void> upgradeToAdmin(@RequestParam("userId") Long userId) {
        permissionService.upgradeToAdmin(userId);
        return ApiResponse.success(null);
    }

    @Override
    @PostMapping("/downgrade-to-user")
    public ApiResponse<Void> downgradeToUser(@RequestParam("userId") Long userId) {
        permissionService.downgradeToUser(userId);
        return ApiResponse.success(null);
    }
}